﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Data;
using System.Collections;
using System.IO;

namespace PromotionEngine.Utils
{
    public class Utils
    {
        public const string IMPORT_PATH = "/Upload/csv/";
        public const string TEMPLATE_PATH = "/Upload/Template/";
        public const string IMPORT_FILENAME = "ImportFile.csv";
        public const string IMPORT_CUSTOMER = "Customer.csv";
        public const string IMPORT_ITEM = "Item.csv";
        public const string IMPORT_WAREHOUSE = "Warehouse.csv";
        public const string IMPORT_SALESPERSON = "SalesPerson.csv";
        public static bool IsNumberInteger(string validateText)
        {
            try
            {
                int.Parse(validateText);
                return true;

            }
            catch
            {
                return false;
            }
        }
        public static bool isDateTime(string validateText)
        {
            try
            {
                DateTime.Parse(validateText);
                return true;

            }
            catch
            {
                return false;
            }
        }
        public static bool IsNumber(string validateText)
        {
            try
            {
                double.Parse(validateText);
                return true;
            }
            catch { return false; }
        }
        public static DataSet IListToDataSet<T>(IList<T> list)
        {
            if (list == null || list.Count <= 0)
            {
                return null;
            }

            DataSet ds = new DataSet();
            DataTable dt = new DataTable(typeof(T).Name);
            DataColumn column;
            DataRow row;

            System.Reflection.PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);

            foreach (T t in list)
            {
                if (t == null)
                {
                    continue;
                }
                row = dt.NewRow();
                for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
                {
                    System.Reflection.PropertyInfo pInfo = myPropertyInfo[i];
                    string name = pInfo.Name;
                    if (dt.Columns[name] == null)
                    {
                        column = new DataColumn(name, Nullable.GetUnderlyingType(pInfo.PropertyType) ?? pInfo.PropertyType);
                        //dt.Columns.Add(column);
                    }

                    row[name] = pInfo.GetValue(t, null) ?? DBNull.Value;
                }

                dt.Rows.Add(row);
            }
            ds.Tables.Add(dt);
            return ds;
        }
        public static DataTable IlistToDataTable(List<string> fields, IList iList)
        {
            DataTable dt = new DataTable();

            foreach (string fieldName in fields)
            {
                dt.Columns.Add(fieldName);
            }


            foreach (object[] values in iList)
            {
                DataRow row = dt.NewRow();
                for (int i = 0; i < fields.Count; i++)
                {
                    row[i] = values[i];
                }
                dt.Rows.Add(row);
            }

            return dt;
        }
        [MethodImpl(MethodImplOptions.NoInlining)]
        public static string GetCurrentMethod()
        {
            StackTrace st = new StackTrace();
            StackFrame sf = st.GetFrame(1);

            return sf.GetMethod().Name;
        }
        public static DataTable ImportCSVFile(string fileName, string fileLocation)
        {
            string[] Lines = File.ReadAllLines(fileLocation + fileName);
            string[] Fields;
            Fields = Lines[0].Split(new char[] { ',' });
            int Cols = Fields.GetLength(0);
            DataTable dt = new DataTable();
            for (int i = 0; i < Cols; i++)
                dt.Columns.Add(Fields[i].ToLower(), typeof(string));
            DataRow Row;
            for (int i = 1; i < Lines.GetLength(0); i++)
            {
                Fields = Lines[i].Split(new char[] { ',' });
                Row = dt.NewRow();
                for (int f = 0; f < Cols; f++)
                    Row[f] = Fields[f];
                dt.Rows.Add(Row);
            }
            return dt;
        }
    }
}